using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;

namespace Narwhunderful.Aphelia.Framework.IO.Serialization
{
    public class PropertyProvider
    {
        private PropertiesToIgnore _propertiesToIgnore;
        private IList<Type> _attributesToIgnore;
        private static readonly Dictionary<Type, IList<PropertyInfo>> _cache = new Dictionary<Type, IList<PropertyInfo>>();

        public PropertiesToIgnore PropertiesToIgnore
        {
            get
            {
                if (_propertiesToIgnore == null) _propertiesToIgnore = new PropertiesToIgnore();
                return _propertiesToIgnore;
            }
            set { _propertiesToIgnore = value; }
        }

        public IList<Type> AttributesToIgnore
        {
            get
            {
                if (_attributesToIgnore == null) _attributesToIgnore = new List<Type>();
                return _attributesToIgnore;
            }
            set { _attributesToIgnore = value; }
        }

        public IList<PropertyInfo> GetProperties(TypeInfo typeInfo)
        {
            // Search in cache
            if (_cache.ContainsKey(typeInfo.Type)) return _cache[typeInfo.Type];

            // Creating infos
            PropertyInfo[] properties = GetAllProperties(typeInfo.Type);
            var result = new List<PropertyInfo>();

            foreach (PropertyInfo property in properties)
            {
                if (!IgnoreProperty(typeInfo, property))
                {
                    result.Add(property);
                }
            }

            // adding result to Cache
            _cache.Add(typeInfo.Type, result);

            return result;
        }

        protected virtual bool IgnoreProperty(TypeInfo info, PropertyInfo property)
        {

            if (PropertiesToIgnore.Contains(info.Type, property.Name))
            {
                return true;
            }

            if (ContainsNonSerializedAttribute(property))
            {
                return true;
            }

            if (!property.CanRead || !property.CanWrite)
            {
                return true;
            }

            ParameterInfo[] indexParameters = property.GetIndexParameters();
            if (indexParameters.Length > 0)
            {
                // Indexer
                return true;
            }

            return false;
        }

        protected bool ContainsNonSerializedAttribute(ICustomAttributeProvider property)
        {
            foreach (Type attrType in AttributesToIgnore)
            {
                object[] attributes = property.GetCustomAttributes(attrType, false);
                if (attributes.Length > 0)
                    return true;
            }
            return false;
        }

        protected virtual PropertyInfo[] GetAllProperties(Type type)
        {
            return type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
        }
    }

    public sealed class PropertiesToIgnore
    {
        private readonly TypePropertiesToIgnoreCollection _propertiesToIgnore = new TypePropertiesToIgnoreCollection();

        public void Add(Type type, string propertyName)
        {
            TypePropertiesToIgnore item = getPropertiesToIgnore(type);
            if (!item.PropertyNames.Contains(propertyName))
            {
                item.PropertyNames.Add(propertyName);
            }
        }

        private TypePropertiesToIgnore getPropertiesToIgnore(Type type)
        {
            TypePropertiesToIgnore item = _propertiesToIgnore.TryFind(type);
            if (item == null)
            {
                item = new TypePropertiesToIgnore(type);
                _propertiesToIgnore.Add(item);
            }
            return item;
        }

        public bool Contains(Type type, string propertyName)
        {
            return _propertiesToIgnore.ContainsProperty(type, propertyName);
        }

        private sealed class TypePropertiesToIgnore
        {
            private IList<string> _propertyNames;

            public TypePropertiesToIgnore(Type type)
            {
                Type = type;
            }

            public Type Type { get; set; }

            public IList<string> PropertyNames
            {
                get
                {
                    if (_propertyNames == null) _propertyNames = new List<string>();
                    return _propertyNames;
                }
                set { _propertyNames = value; }
            }
        }

        private sealed class TypePropertiesToIgnoreCollection : KeyedCollection<Type, TypePropertiesToIgnore>
        {
            protected override Type GetKeyForItem(TypePropertiesToIgnore item)
            {
                return item.Type;
            }

            public int IndexOf(Type type)
            {
                for (int i = 0; i < Count; i++)
                {
                    TypePropertiesToIgnore item = this[i];
                    if (item.Type == type)
                    {
                        return i;
                    }
                }
                return -1;
            }

            public TypePropertiesToIgnore TryFind(Type type)
            {
                foreach (TypePropertiesToIgnore item in Items)
                {
                    if (item.Type == type) return item;
                }
                return null;
            }

            public bool ContainsProperty(Type type, string propertyName)
            {
                TypePropertiesToIgnore propertiesToIgnore = TryFind(type);
                if (propertiesToIgnore == null) return false;
                return propertiesToIgnore.PropertyNames.Contains(propertyName);
            }
        }

    }

}
